//
//  RegisterController.m
//  QQplayground
//
//  Created by zhangxy on 2018/5/24.
//  Copyright © 2018年 zhang. All rights reserved.
//

#import "RegisterController.h"

@interface RegisterController ()


@property(nonatomic,strong) ZTextField *txtName;
@property(nonatomic,strong) UILabel *labName;

@property(nonatomic,strong) ZTextField *txtPwd;

@property(nonatomic,strong) UILabel *labPwd;
@end

@implementation RegisterController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIImage *backImage =[UIImage imageNamed:@"base_bg.jpg"];
    self.view.layer.contents=(id)backImage.CGImage;
    //    self.view.layer.backgroundColor = [UIColor clearColor].CGColor;
    self.view.layer.backgroundColor = UIColorFromRGB(0x67ACA6).CGColor;
    
    
    [self createTitleMenu];
    [self.menuTitleButton setTitle:@"设置密码" forState:0];
    [self.titleMenu setBackgroundColor:UIColor.clearColor];
    
    [self.menuLeftButton setImage:[UIImage imageNamed:@"btn_back_title_sel"] forState:UIControlStateNormal];
    [self.menuLeftButton setImage:[UIImage imageNamed:@"btn_back_title_sel"] forState:UIControlStateHighlighted];
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenKeyboard:)];
    [self.view addGestureRecognizer:tap];
    
    CGFloat startY = 124;
    
    UIImageView *iconView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth/2 - 100, startY, 200, 30)];
    [iconView setImage:[UIImage imageNamed:@"qzone"]];
    [iconView setContentMode:UIViewContentModeScaleAspectFit];
    [iconView setBackgroundColor:UIColor.clearColor];
    [self.view addSubview:iconView];
    
    
    startY = 221;
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(20, startY, ScreenWidth - 40, 21)];
    [lab setText:@"设置密码后可使用手机号+密码的方式登录"];
    [lab setFont:FontFiftent];
    [lab setTextColor:UIColorFromRGB(TextWhiteColor)];
    
    startY = startY + 29;
    _txtPwd = [ZZViewUtils createZTextField:@"请设置密码(6-16位)" padding:20];
    [_txtPwd setFrame:CGRectMake(20, startY, ScreenWidth-40, 52)];
    _txtPwd.secureTextEntry = YES;
    
    startY = startY + 62;
    
    _labPwd = [[UILabel alloc] initWithFrame:CGRectMake(20, startY, ScreenWidth - 40, 21)];
    [_labPwd setText:@"密码长度为6到16个字符"];
    [_labPwd setFont:FontThirteen];
    [_labPwd setTextColor:UIColorFromRGB(TextWhiteColor)];
    
    startY = startY + 21 + 22;
    
    _txtName = [ZZViewUtils createZTextField:@"昵称" padding:20];
    [_txtName setFrame:CGRectMake(20, startY, ScreenWidth-40, 52)];
    
    startY = startY + 62;
    
    _labName = [[UILabel alloc] initWithFrame:CGRectMake(20, startY, ScreenWidth - 40, 21)];
    [_labName setText:@"xxxxxx"];
    [_labName setFont:FontThirteen];
    [_labName setTextColor:UIColorFromRGB(TextWhiteColor)];
    
    
    [self.view addSubview:_txtPwd];
    if(_isRegister){
        [self.view addSubview:lab];
        [self.view addSubview:_labPwd];
        [self.view addSubview:_labName];
        [self.view addSubview:_txtName];
    }

    startY = startY + 51;
    UIButton *_preButton = [self createButton:@""];
    [_preButton setFrame:CGRectMake(ScreenWidth/2 - 121/2, startY, 110, 52)];
    [_preButton setBackgroundColor:UIColorFromRGB(TextWhiteColor)];
    _preButton.layer.cornerRadius = 29.0f;
    _preButton.layer.masksToBounds = YES;
    [_preButton setTitle:@"完成" forState:0];
    [_preButton setTitleColor:UIColorFromRGB(BgTitleColor) forState:0];
    [_preButton.titleLabel setFont:ListTitleFont];
    _preButton.tag = 12;
    [_preButton addTarget:self action:@selector(buttonComplete:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_preButton];
}


-(void)buttonComplete:(UIButton *) btn{
    if(_isRegister){
        if(convertToString(_txtName.text).length < 2){
            [PopUtil showMessage:@"请输入正确的昵称"];
            return;
        }
        
        if(convertToString(_txtPwd.text).length < 6){
            [PopUtil showMessage:@"请输入正确的密码"];
            return;
        }
        
        NSDictionary *p = @{@"phone":convertToString(_phone),
                            @"phone_area_code":convertToString(_phone_area_code),
                            @"sms_code":convertToString(_phone_code),
                            @"password":convertToString(_txtPwd.text),
                            @"user_name":convertToString(_txtName.text)
                            };
        [NetworkUtil POST:API_Register parameters:p success:^(id responseObject, NSDictionary *dict) {
            User *msg = [[User alloc] initWithDictionary:dict[@"data"][@"user_info"]];
            msg.isOnline = YES;
            [UserUtil saveUser:msg];
            User *user  =  [UserUtil getUser];
            if (user.isOnline) {
                self.view.window.rootViewController = [[BaseTabBarController alloc] init];
            }
        } failure:^(NSError *error, NSString *message) {
            [PopUtil showMessage:message];
        }];
    }else{
        NSDictionary *p = @{@"phone":convertToString(_phone),
                            @"phone_area_code":convertToString(_phone_area_code),
                            @"sms_code":convertToString(_phone_code),
                            @"password":convertToString(_txtPwd.text)
                            };
        [NetworkUtil POST:API_FindPwd parameters:p success:^(id responseObject, NSDictionary *dict) {
//            [PopUtil showMessage:dict[@"msg"]];
            [self.navigationController popToRootViewControllerAnimated:YES];
        } failure:^(NSError *error, NSString *message) {
            NSLog(@"message++++++++++++++++%@",message);
            [PopUtil showMessage:message];
        }];
    }
}



-(UIButton *) createButton:(NSString *) imageName{
    UIButton *btn = ({
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setImage:[UIImage imageNamed:convertToString(imageName)] forState:0];
        backBtn;
    });
    return btn;
}

// 隐藏键盘
-(void)hiddenKeyboard:(id)sender
{
    [_txtName resignFirstResponder];
    [_txtPwd resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
